/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaOperaciones.java
 *
 * Created on 02-nov-2011, 9:34:43
 */
package aerodromo.view;

import aerodromo.controller.LogController;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Oswaldo.C
 */
public class VentanaOperaciones extends javax.swing.JDialog {

    /** Creates new form VentanaOperaciones */
    public VentanaOperaciones(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo( parent );
        
        
        controladorLog = new LogController();
        atxtOperaciones.setText( controladorLog.getBitacora() );
        
        setVisible( true );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbTitulo = new javax.swing.JLabel();
        btnAlmacenar = new javax.swing.JButton();
        btnRegresar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        atxtOperaciones = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Log Operaciones");
        setModal(true);
        setResizable(false);

        lbTitulo.setFont(new java.awt.Font("Tahoma", 1, 14));
        lbTitulo.setText("BITÁCORA DE OPERACIONES");

        btnAlmacenar.setText("ALMACENAR");
        btnAlmacenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlmacenarActionPerformed(evt);
            }
        });

        btnRegresar.setText("REGRESAR");
        btnRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegresarActionPerformed(evt);
            }
        });

        atxtOperaciones.setColumns(20);
        atxtOperaciones.setEditable(false);
        atxtOperaciones.setRows(5);
        jScrollPane1.setViewportView(atxtOperaciones);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(178, 178, 178)
                        .addComponent(lbTitulo))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(btnAlmacenar)
                        .addGap(126, 126, 126)
                        .addComponent(btnRegresar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 522, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(lbTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAlmacenar)
                    .addComponent(btnRegresar))
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegresarActionPerformed
   dispose();
}//GEN-LAST:event_btnRegresarActionPerformed

private void btnAlmacenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlmacenarActionPerformed
   //Almacenar en disco el archivo de logs
    
    //Usar un file chooser para elegir la ubicacion
    String log = atxtOperaciones.getText();
        try {
            controladorLog.saveLog(log);
            JOptionPane.showMessageDialog( this , "El registro de operaciones se ha guardado con éxito" , "Operación Terminada", JOptionPane.INFORMATION_MESSAGE);
        } catch (FileNotFoundException ex) {
           JOptionPane.showMessageDialog( this , "Error: Archivo no encontrado", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
           JOptionPane.showMessageDialog( this , "Error: Imposible escribir archivo", "Error", JOptionPane.ERROR_MESSAGE); 
        }
        
    dispose();
        
}//GEN-LAST:event_btnAlmacenarActionPerformed


    private LogController controladorLog;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea atxtOperaciones;
    private javax.swing.JButton btnAlmacenar;
    private javax.swing.JButton btnRegresar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTitulo;
    // End of variables declaration//GEN-END:variables
}
